const GAME_OBJECT = [];

export class GameObject{
    constructor(){
        GAME_OBJECT.push(this);
        this.has_called_start = false;
        this.timedelt = 0;

    }
    start(){//只执行一次

    }

    update(){//除第一帧之外，每一帧执行

    }
    on_destroy(){

    }
    destroy(){
        for(let i in GAME_OBJECT){
            const obj = GAME_OBJECT[i];
            if(obj === this){
                GAME_OBJECT.splice(i);
                break;
            }
        }
    }


}

let last_stamp;//上一次执行的时刻
const step = timestamp =>{//timestamp 现在的时刻
    for(let obj of GAME_OBJECT)
    if(!obj.has_called_start){
        obj.has_called_start = true;
        obj.start();
    }else{
        obj.timedelt = timestamp - last_stamp
        obj.update();
    }
    last_stamp = timestamp;
    requestAnimationFrame(step);
}

requestAnimationFrame(step);